package com.letv.so.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

public class DateUtil {
	/**
	 * 单例
	 */
	private static DateUtil dateUtil;

	private DateUtil() {
	}

	/**
	 * 单例模式，获得实例
	 */
	public synchronized static DateUtil getInstance() {
		if (dateUtil == null) {
			dateUtil = new DateUtil();
		}
		return dateUtil;
	}

	/**
	 * 返回时间格式 yyyy-MM-dd hh:mm:ss
	 * 
	 * @param Date
	 *            date
	 * @return
	 */
	public String formatTime(Date date) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		return sdf.format(date);
	}

	/**
	 * 返回时间格式 yyyy-MM-dd hh:mm:ss
	 * 
	 * @param Long
	 *            time
	 * @return
	 */
	public String formatTime(Long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		return sdf.format(new Date(time));
	}

	public static long getTime(String str) {
		if (StringUtils.isBlank(str)) {
			return 0;
		}
		SimpleDateFormat df;
		if (str.length() == 4) {
			df = new SimpleDateFormat("yyyy");
		} else if (str.length() == 10) {
			df = new SimpleDateFormat("yyyy-MM-dd");
		} else {
			df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		}

		Date date;
		try {
			date = df.parse(str);
			return date.getTime();
		} catch (ParseException e) {
			return 0;
		}
	}

	/**
	 * 判断是否是前几月
	 * 
	 * @param time
	 * @param nonth
	 * @return
	 */
	public boolean isTrue(String time, int nonth) {
		if (time == null || time.isEmpty()) {
			return true;
		}
		try {
			return (System.currentTimeMillis() - new Long(time)) >= (nonth * 1000 * 60 * 60 * 30 * 24L);
		} catch (Exception ex) {
			try {
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
				long date = format1.parse(time).getTime();
				return (System.currentTimeMillis() - date) >= (nonth * 1000 * 60 * 60 * 30 * 24L);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
	}

	/**
	 * 时间计算
	 * 
	 * @param date
	 * @param addMonth
	 * @param addDay
	 * @param addHour
	 * @return
	 */
	public String dataCompute(Date date, int addMonth, int addDay, int addHour) {
		SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR);
		calendar.set(Calendar.MONTH, month + addMonth);
		calendar.set(Calendar.DATE, day + addDay);
		calendar.set(Calendar.HOUR, hour + addHour);
		Date cc = calendar.getTime();
		return form.format(cc);
	}

	public String monthAdd(String date, int addMonth) {
		return dataCompute(stringToDate(date), addMonth, 0, 0);
	}

	public String dayAdd(String date, int addDate) {
		return dataCompute(stringToDate(date), 0, addDate, 0);
	}

	public String hourAdd(String date, int addHour) {
		return dataCompute(stringToDate(date), 0, 0, addHour);
	}

	public String timeAdd(String date, int addMonth, int addDay, int addHour) {
		return dataCompute(stringToDate(date), addMonth, 0, 0);
	}

	/**
	 * 字符串 to Date
	 * 
	 * @param time
	 * @return
	 */
	public Date stringToDate(String time) {
		SimpleDateFormat df = null;
		try {
			if (time.length() == 4) {
				df = new SimpleDateFormat("yyyy", Locale.CHINA);
			} else if (time.length() == 7) {
				df = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
			} else if (time.length() > 7 && time.length() <= 10) {
				df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
			} else if (time.length() == 20 || time.length() == 18) {
				df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
				time = time.replace(".0", "");
			}else {
				df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
			}
			return df.parse(time);
		} catch (ParseException e) {
			return new Date(0);
		}
	}

	/**
	 * 判断当前日期是否为星期一
	 * 
	 * @return
	 */
	public boolean isMonday() {
		return (Calendar.getInstance(Locale.CANADA).get(Calendar.DAY_OF_WEEK) - 1) == 1 ? true : false;
	}

	public static void main(String[] args) throws ParseException {
		//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		System.out.println(getTime("2001"));
		System.out.println(getTime("2000"));
		System.out.println(getTime("1999"));
		System.out.println(getTime("1979"));
		
		/*DateUtil dataUtil=DateUtil.getInstance();
		String str="1364376507000";
		Long time=Long.valueOf(str);
		System.out.println("ori:"+time);
		
		String mtime=dataUtil.formatTime(time);
		System.out.println(mtime);*/
		

	
	}
}
